{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python 基础语法精要\n",
    "\n",
    "本笔记本系统回顾Python核心语法，包含以下内容：\n",
    "1. 变量与数据类型\n",
    "2. 流程控制（条件语句与循环）\n",
    "3. 函数定义与使用\n",
    "4. 常用数据结构操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. 变量与数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1 基础数据类型\n",
    "- **int**: 整型（例：42）\n",
    "- **float**: 浮点型（例：3.14）\n",
    "- **str**: 字符串（例：\"Hello\"）\n",
    "- **bool**: 布尔型（True/False）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'str'> <class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "# 类型验证示例\n",
    "integer_var = 10\n",
    "float_var = 10.5\n",
    "string_var = 'Python教程'\n",
    "bool_var = True\n",
    "\n",
    "print(type(integer_var), type(float_var), \n",
    "      type(string_var), type(bool_var))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2 字符串操作\n",
    "- 大小写转换\n",
    "- 字符串分割\n",
    "- 切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Original:  Python String Operations \n",
      "UPPER:  PYTHON STRING OPERATIONS \n",
      "lower:  python string operations \n",
      "Title: Python String Operations\n",
      "Split: ['', 'Python', 'String', 'Operations', '']\n",
      "Sliced: ython St\n"
     ]
    }
   ],
   "source": [
    "demo_str = \" Python String Operations \"\n",
    "\n",
    "# 大小写转换\n",
    "print(\"Original:\", demo_str)\n",
    "print(\"UPPER:\", demo_str.upper())\n",
    "print(\"lower:\", demo_str.lower())\n",
    "print(\"Title:\", demo_str.strip().title())\n",
    "\n",
    "# 分割与切片\n",
    "print(\"Split:\", demo_str.split(' '))\n",
    "print(\"Sliced:\", demo_str[2:10])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3 复合数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 列表（List）\n",
    "- 可变序列\n",
    "- 支持推导式\n",
    "- 解包操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Squared: [1, 4, 9, 16, 25]\n",
      "Unpacked: 1, 2, rest=[3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "# 列表基础操作\n",
    "num_list = [1, 2, 3, 4, 5]\n",
    "\n",
    "\n",
    "# 列表推导式\n",
    "squared = [x**2 for x in num_list]\n",
    "print(\"Squared:\", squared)\n",
    "\n",
    "# 解包操作\n",
    "a, b, *rest = num_list\n",
    "print(f\"Unpacked: {a}, {b}, rest={rest}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 元组（Tuple）与集合（Set）\n",
    "- 元组：不可变序列\n",
    "- 集合：无序不重复元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Tuple: (10.5, 20.3)\n",
      "Set: {1, 2, 3, 4, 5}\n"
     ]
    }
   ],
   "source": [
    "# 元组示例\n",
    "coordinates = (10.5, 20.3)\n",
    "print(\"Tuple:\", coordinates)\n",
    "\n",
    "# 集合操作\n",
    "unique_nums = {1, 2, 2, 3, 4}\n",
    "unique_num_list = set(num_list)\n",
    "unique_nums.add(5)\n",
    "print(\"Set:\", unique_nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 字典（Dictionary）\n",
    "- 键值对存储\n",
    "- 高效查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_profile = {\n",
    "    \"name\": \"Alice\",\n",
    "    \"age\": 30,\n",
    "    \"skills\": [\"Python\", \"SQL\"]\n",
    "}\n",
    "\n",
    "# 字典操作\n",
    "user_profile[\"email\"] = \"alice@example.com\"\n",
    "print(\"Keys:\", user_profile.keys())\n",
    "print(\"Items:\")\n",
    "for k, v in user_profile.items():\n",
    "    print(f\"{k}: {v}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. 流程控制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1 条件语句\n",
    "```python\n",
    "if condition:\n",
    "    # true branch\n",
    "elif other_condition:\n",
    "    # elif branch\n",
    "else:\n",
    "    # false branch\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "良好\n"
     ]
    }
   ],
   "source": [
    "# 条件判断示例\n",
    "score = 85\n",
    "\n",
    "if score >= 90:\n",
    "    print(\"优秀\")\n",
    "elif score >= 80:\n",
    "    print(\"良好\")\n",
    "else:\n",
    "    print(\"继续努力\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 循环结构\n",
    "#### for循环\n",
    "```python\n",
    "for item in iterable:\n",
    "    # process item\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Language 1: Python\n",
      "Language 2: Java\n",
      "Language 3: C++\n",
      "--------------\n",
      "Language: Python\n",
      "Language: Java\n",
      "Language: C++\n"
     ]
    }
   ],
   "source": [
    "# 遍历示例\n",
    "languages = [\"Python\", \"Java\", \"C++\"]\n",
    "\n",
    "for idx, lang in enumerate(languages, 1):\n",
    "    print(f\"Language {idx}: {lang}\")\n",
    "    \n",
    "print('--------------')\n",
    "\n",
    "for it in languages:\n",
    "    print(f\"Language: {it}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. 函数\n",
    "\n",
    "### 函数定义\n",
    "```python\n",
    "def function_name(parameters):\n",
    "    '''函数文档'''\n",
    "    # function body\n",
    "    return result\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "圆面积: 78.53975\n"
     ]
    }
   ],
   "source": [
    "# 函数示例\n",
    "def calculate_circle_area(radius):\n",
    "    \"\"\"计算圆形面积\"\"\"\n",
    "    return 3.14159 * radius ** 2\n",
    "\n",
    "print(\"圆面积:\", calculate_circle_area(5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.1 Lambda函数\n",
    "- 匿名函数\n",
    "- 简洁语法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Lambda示例\n",
    "square = lambda x: x ** 2\n",
    "print(\"平方运算:\", square(4))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
